<?php

namespace Agp\CoreBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;

use APY\DataGridBundle\Grid\Mapping as GRID;

/**
 * Agp\CoreBundle\Entity\Atendente
 *
 * CLASSE
 * @ORM\Table(name="atendente")
 * @ORM\Entity(repositoryClass="Agp\CoreBundle\Repository\AtendenteRepository")
 * @UniqueEntity(fields={"empresaInc", "empresa"})
 * @UniqueEntity(fields={"nome", "empresa"})
 * @ORM\HasLifecycleCallbacks
 * 
 * @GRID\Source(columns="codAtendente, empresaInc, nome, vlrFixo, comissao, situacao")
 */
class Atendente
{
    /**
     * @var integer $codAtendente
     *
     * @ORM\Column(name="cod_atendente", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     *
     * @GRID\Column(title="Código", visible=false, type="text", size="5", primary=true)
     */
    protected $codAtendente;

    /**
     * @var object $empresa
     *
     * @ORM\ManyToOne(targetEntity="Empresa", fetch="LAZY")
     * @ORM\JoinColumn(name="cod_empresa", referencedColumnName="cod_empresa", onDelete="Cascade", nullable=false)
     */
    protected $empresa;

    /**
     * @var string $nome
     *
     * @ORM\Column(name="nome", type="string", length=42, nullable=false)
     * @Assert\NotBlank()
     * @Assert\Length(
     *      max = "21"
     * )
     * 
     * @GRID\Column(title="Nome", type="text")
     */
    protected $nome;

    /**
     * @var int $senha
     *
     * @ORM\Column(name="senha", type="integer", length=6, nullable=true)
     * @Assert\NotBlank()
     * @Assert\Length(
     *      max = "6"
     * )
     */
    protected $senha;
    
    /**
     * @var int $comissao
     *
     * @ORM\Column(name="comissao", type="integer", length=6, nullable=false)
     * @Assert\NotBlank()
     * @Assert\Length(
     *      max = "2"
     * )
     * 
     * @GRID\Column(title="Comissão", type="number", style="percent", align="center")
     */
    protected $comissao;
    
    /**
     * @var float $vlrBase
     *
     * @ORM\Column(name="vlr_fixo", type="float", nullable=true)
     * @Assert\NotBlank
     * 
     * @GRID\Column(title="Valor fixo", type="number", style="currency", align="center")
     */
    protected $vlrFixo;
    
    /**
     *
     * @var integer $empresaInc
     * @ORM\Column(name="empresa_inc", type="integer", nullable=false)
     * 
     * @GRID\Column(title="Código", type="text", align="center")
     */
    protected $empresaInc;
    
    const SIT_ATIVO = 'Ativo';
    const SIT_INATIVO = 'Inativo';
    /**
     * @var string $situacao
     *
     * @ORM\Column(name="situacao", type="string", nullable=false)
     * @GRID\Column(title="Situação", type="text", align="center")
     */
    protected $situacao;
    
    /**
     * @ORM\ManyToMany(targetEntity="Agp\CoreBundle\Entity\TerminalEmpresa")
     * @ORM\JoinTable(name="atendente_terminal_empresa",
     *     joinColumns={@ORM\JoinColumn(name="cod_atendente", referencedColumnName="cod_atendente", onDelete="Cascade")},
     *     inverseJoinColumns={@ORM\JoinColumn(name="cod_terminal_empresa", referencedColumnName="cod_terminal_empresa", onDelete="Cascade")}
     * )
     * @var ArrayCollection $terminais
     */ 
    protected $terminais;
    
    /**
     * @ORM\Column(name="tipo", type="string", length=45, nullable=false)
     * @var type string
     */
    protected $tipo;
    const TP_ATEN = "Atendente";
    const TP_COM = "Comissionado";
    const TP_PDV = "Ponto de Venda";
    
    /**
     * @var string $endereco
     *
     * @ORM\Column(name="endereco", type="string", length=45, nullable=true)
     */
    protected $endereco;

    /**
     * @var string $complemento
     *
     * @ORM\Column(name="complemento", type="string", length=45, nullable=true)
     */
    protected $complemento;

    /**
     * @var string $cep
     *
     * @ORM\Column(name="cep", type="string", length=45, nullable=true)
     */
    protected $cep;

    /**
     * @var string $bairro
     *
     * @ORM\Column(name="bairro", type="string", length=45, nullable=true)
     */
    protected $bairro;

    /**
     * @var integer $cidade
     *
     * @ORM\ManyToOne(targetEntity="Cidade", cascade={"persist"}, fetch="LAZY")
     * @ORM\JoinColumn(name="cod_cidade", referencedColumnName="id", onDelete="restrict", nullable=true)
     */
    protected $cidade;
    
    /**
     * @var string $email
     *
     * @ORM\Column(name="email", type="text", nullable=true)
     */
    protected $email;
    
    /**
     * @var string $email
     *
     * @ORM\Column(name="telefone", type="string", length=45, nullable=true)
     */
    protected $telefone;
    
    /**
     * @var string $email
     *
     * @ORM\Column(name="celular", type="string", length=45, nullable=true)
     */
    protected $celular;

    /* hack for Acls */
    public function getId(){
        return 'class';
    }
    
    public function __construct() {
        $this->situacao = self::SIT_ATIVO;
        $this->comissao = 0;
        $this->vlrFixo = 0;
    }

    public function __toString() {
        return $this->nome;
    }
    
    /**
     * @ORM\PreUpdate
     */
    public function preUpdate(){
        if ($this->tipo != self::TP_PDV){
            $this->terminais->clear();
        }
        
    }

    /**
     * Set codAtendente
     *
     * @param integer $codAtendente
     * @return Atendente
     */
    public function setCodAtendente($codAtendente)
    {
        $this->codAtendente = $codAtendente;
    
        return $this;
    }

    /**
     * Get codAtendente
     *
     * @return integer 
     */
    public function getCodAtendente()
    {
        return $this->codAtendente;
    }

    /**
     * Set empresa
     *
     * @param integer $empresa
     * @return Atendente
     */
    public function setEmpresa($empresa)
    {
        $this->empresa = $empresa;
    
        return $this;
    }

    /**
     * Get empresa
     *
     * @return integer 
     */
    public function getEmpresa()
    {
        return $this->empresa;
    }

    /**
     * Set nome
     *
     * @param string $nome
     * @return Atendente
     */
    public function setNome($nome)
    {
        $this->nome = mb_strtoupper($nome, 'UTF-8');
    
        return $this;
    }

    /**
     * Get nome
     *
     * @return string 
     */
    public function getNome()
    {
        return $this->nome;
    }

    /**
     * Set senha
     *
     * @param string $senha
     * @return Atendente
     */
    public function setSenha($senha)
    {
        $this->senha = $senha;
    
        return $this;
    }

    /**
     * Get senha
     *
     * @return string 
     */
    public function getSenha()
    {
        return $this->senha;
    }
    
    public function getComissao() {
        return $this->comissao;
    }

    public function setComissao($comissao) {
        $this->comissao = $comissao;
    }

    public function getEmpresaInc() {
        return $this->empresaInc;
    }

    public function setEmpresaInc($empresaInc) {
        $this->empresaInc = $empresaInc;
    }
    
    public function getSituacao() {
        return $this->situacao;
    }

    public function setSituacao($situacao) {
        $this->situacao = $situacao;
    }
    
    public function isAtivo(){
        return ($this->situacao == self::SIT_ATIVO);

    }
    
    public function getTerminais() {
        return $this->terminais;
    }

    public function setTerminais($terminais) {
        return $this->terminais = $terminais;
    }
    
    public function addTerminal($terminal) {
        $this->terminais[] = $terminal;
    }
    
    public function getTipo() {
        return $this->tipo;
    }

    public function setTipo($tipo) {
        $this->tipo = $tipo;
    }

    public static function getTipoList(){
        return array(self::TP_ATEN => self::TP_ATEN,
                     self::TP_COM => self::TP_COM,
                     self::TP_PDV => self::TP_PDV);
    }
    
    public function getEndereco() {
        return $this->endereco;
    }

    public function setEndereco($endereco) {
        $this->endereco = $endereco;
    }

    public function getComplemento() {
        return $this->complemento;
    }

    public function setComplemento($complemento) {
        $this->complemento = $complemento;
    }

    public function getCep() {
        return $this->cep;
    }

    public function setCep($cep) {
        $this->cep = $cep;
    }

    public function getBairro() {
        return $this->bairro;
    }

    public function setBairro($bairro) {
        $this->bairro = $bairro;
    }

    public function getCidade() {
        return $this->cidade;
    }

    public function setCidade($cidade) {
        $this->cidade = $cidade;
    }

    public function getEmail() {
        return $this->email;
    }

    public function setEmail($email) {
        $this->email = $email;
    }

    public function getTelefone() {
        return $this->telefone;
    }

    public function setTelefone($telefone) {
        $this->telefone = $telefone;
    }

    public function getCelular() {
        return $this->celular;
    }

    public function setCelular($celular) {
        $this->celular = $celular;
    }
    
    public function getVlrFixo() {
        return $this->vlrFixo;
    }

    public function setVlrFixo($vlrFixo) {
        $this->vlrFixo = substr(str_replace(",", ".", $vlrFixo),3);
    }


}
